package com.example.goodpet.chat.activity;

import androidx.fragment.app.FragmentActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.goodpet.R;
import com.example.goodpet.chat.broadcastreceiver.MessageReceivedReceiver;
import com.example.goodpet.chat.fragment.ChatFragment;
import com.example.goodpet.utils.ActivityManager;
import com.hyphenate.easeui.EaseIM;
import com.hyphenate.easeui.constants.EaseConstant;
import com.hyphenate.easeui.domain.EaseUser;
import com.hyphenate.easeui.provider.EaseUserProfileProvider;
import com.zackratos.ultimatebarx.ultimatebarx.java.UltimateBarX;


public class ChatActivity extends FragmentActivity {
    private TextView titleText;
    private ImageView cmTitleLeft;
    private TextView cmTitleRight;
    private String conversationId;
    private int chatType;
    //private EaseChatFragment chatFragment;
    private ChatFragment chatFragment;
    private String historyMsgId;

    //private ChatViewModel viewModel;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setContentView(R.layout.activity_chat);
        UltimateBarX.statusBarOnly(this)
                .fitWindow(true)
                .colorRes(R.color.ic_back_background)
                .light(true)
                .apply();
        ActivityManager.getInstance().addActivity(this);

        cmTitleLeft=findViewById(R.id.cmTitleLeft);
        titleText=findViewById(R.id.cmTitleText);
        cmTitleRight=findViewById(R.id.cmTitleRight);


        initIntent();
        initChatFragment();
        initListener();
        initDefaultTitle();

        //resetChatExtendMenu();
    }

    private void initDefaultTitle() {
        cmTitleRight.setVisibility(View.INVISIBLE);

        String title;
            EaseUserProfileProvider userProvider = EaseIM.getInstance().getUserProvider();
            if(userProvider != null) {
                EaseUser user = userProvider.getUser(conversationId);
                if(user != null) {
                    title = user.getNickname();
                }else {
                    title = conversationId;
                }
            }else {
                title = conversationId;
            }
        titleText.setText(title);
    }

    private void initListener() {
        cmTitleLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        ActivityManager.getInstance().removeActivity(ChatActivity.this);
        //监听刷新
        Intent intent=new Intent(MessageReceivedReceiver.BROAD_CAST_MSG_RECEIVED);
        intent.setPackage(getPackageName());
        sendBroadcast(intent);
    }

    private void initChatFragment() {
        //use EaseChatFratFragment
        //chatFragment = new EaseChatFragment();
        chatFragment=new ChatFragment();
        //pass parameters to chat fragment
        chatFragment.setArguments(getIntent().getExtras());
        getSupportFragmentManager().beginTransaction().add(R.id.container, chatFragment).commit();
    }

    private void initIntent() {
        Intent intent = getIntent();
        conversationId = intent.getStringExtra(EaseConstant.EXTRA_CONVERSATION_ID);
        chatType = intent.getIntExtra(EaseConstant.EXTRA_CHAT_TYPE, EaseConstant.CHATTYPE_SINGLE);
        historyMsgId = intent.getStringExtra(EaseConstant.HISTORY_MSG_ID);
    }

    public static void actionStart(Context context, String conversationId, int chatType) {
        Intent intent = new Intent(context, ChatActivity.class);
        intent.putExtra(EaseConstant.EXTRA_CONVERSATION_ID, conversationId);
        intent.putExtra(EaseConstant.EXTRA_CHAT_TYPE, chatType);
        context.startActivity(intent);
    }

}